#!/usr/bin/env python

"""
This is a script to convert the cvf file from gmail contacts to .mutt.alias
by yownd511 AT gmail.com
"""

import os, sys

def parse_read(f):
	block = None
	contact_dic = {}
	email = []
	for i in f:
		if i.startswith("BEGIN"):
			f.next()
			name = f.next().split(":")[1]
		elif i.startswith("EMAIL"):
			email.append(i.split(":")[1])
		elif i.startswith("END"):
			contact_dic[name] = email
			name = None
			email = []
	return contact_dic

def main():
	fin = open("contacts.vcf", 'r')
	fout = open("mutt.alias", 'w')
	dic = parse_read(fin)
	for i in dic:
		for j in range(len(dic[i])):
			fout.write("alias xx "+i[:-2]+" "+dic[i][j][:-2]+"\n")
	fout.close()
	fin.close()

if __name__=='__main__':
	main()
